/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.contract.dto.AppContractOptionalSymbolListDTO;
import com.pig4cloud.pigx.contract.entity.ContractOptionalSymbol;
import com.pig4cloud.pigx.contract.service.ContractOptionalSymbolService;
import com.pig4cloud.pigx.contract.vo.AppContractSymbolIncreaseRankingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 自选行情表
 *
 * @date 2022-08-12 11:42:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/contractOptionalSymbol")
@Api(value = "contractOptionalSymbol", tags = "自选行情表管理")
public class ContractOptionalSymbolController {

	private final ContractOptionalSymbolService contractOptionalSymbolService;

	/**
	 * 分页查询
	 * @param dto dto
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping("/list")
	public R getContractOptionalSymbolPage(@RequestBody AppContractOptionalSymbolListDTO dto) {
		List<AppContractSymbolIncreaseRankingVO> list = contractOptionalSymbolService.getList(dto);
		return R.ok(list);
	}


	/**
	 * 通过id查询自选行情表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('contract_contractoptionalsymbol_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(contractOptionalSymbolService.getById(id));
	}

	/**
	 * 新增自选行情表
	 *
	 * @param contractOptionalSymbol 自选行情表
	 * @return R
	 */
	@ApiOperation(value = "新增自选行情表", notes = "新增自选行情表")
	@SysLog("新增自选行情表")
	@PostMapping
	public R save(@RequestBody ContractOptionalSymbol contractOptionalSymbol) {
		contractOptionalSymbol.setIsDeleted("0");
		// 获取当前用户
		PigxUser pigxUser = SecurityUtils.getUser();
		contractOptionalSymbol.setUserId(pigxUser.getId());
		return R.ok(contractOptionalSymbolService.save(contractOptionalSymbol));
	}

	/**
	 * 批量新增自选行情表
	 *
	 * @param list 自选行情表
	 * @return R
	 */
	@ApiOperation(value = "批量新增自选行情表", notes = "批量新增自选行情表")
	@SysLog("批量新增自选行情表")
	@PostMapping("/batch")
	public R save(@RequestBody List<ContractOptionalSymbol> list) {
		try {
			// 获取当前用户
			PigxUser pigxUser = SecurityUtils.getUser();
			list.forEach(e -> {
				e.setUserId(pigxUser.getId());
				e.setIsDeleted("0");
				contractOptionalSymbolService.save(e);
			});
			return R.ok();
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}

	}

	/**
	 * 修改自选行情表
	 *
	 * @param contractOptionalSymbol 自选行情表
	 * @return R
	 */
	@ApiOperation(value = "修改自选行情表", notes = "修改自选行情表")
	@SysLog("修改自选行情表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('contract_contractoptionalsymbol_edit')")
	public R updateById(@RequestBody ContractOptionalSymbol contractOptionalSymbol) {
		return R.ok(contractOptionalSymbolService.updateById(contractOptionalSymbol));
	}

	/**
	 * 通过id删除自选行情表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除自选行情表", notes = "通过id删除自选行情表")
	@SysLog("通过id删除自选行情表")
	@DeleteMapping("/{symbolId}")
	public R removeById(@PathVariable("symbolId") Long id) {
		PigxUser pigxUser = SecurityUtils.getUser();
		QueryWrapper<ContractOptionalSymbol> wrapper = new QueryWrapper<>();
		wrapper.eq("symbol_id",id);
		wrapper.eq("user_id",pigxUser.getId());
		return R.ok(contractOptionalSymbolService.remove(wrapper));
	}
}
